package com.nanyang.tms.manager.waybill.handler;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.nanyang.common.core.flow.context.TransHandlerContext;
import com.nanyang.common.core.flow.handler.AbstractTransHandler;
import com.nanyang.common.tool.utils.convert.BeanUtils;
import com.nanyang.common.tool.utils.date.DateUtils;
import com.nanyang.common.tool.utils.uuid.GlobalIdGeneratorUtil;
import com.nanyang.tms.common.contstant.TmsSerializeConstant;
import com.nanyang.tms.common.util.TmsExceptionUtil;
import com.nanyang.tms.dao.waybill.WaybillIncomeFeeDao;
import com.nanyang.tms.domain.forecast.context.CalculateFreightContext;
import com.nanyang.tms.domain.waybill.WaybillIncomeFeeDO;
import com.nanyang.tms.domain.waybill.context.SaveTackInfoContext;
import com.nanyang.tms.domain.waybill.context.SaveWaybillReceiptContext;

import lombok.extern.slf4j.Slf4j;
@Component
@Slf4j
public class SaveIncomeHandler  extends AbstractTransHandler {
    @Autowired
	private WaybillIncomeFeeDao waybillIncomeFeeDao;
	@Autowired
	private GlobalIdGeneratorUtil globalIdGeneratorUtil;
	@Override
	public boolean isAsync() {
		return false;
	}

	@Override
	public boolean handle(TransHandlerContext context) {
		CalculateFreightContext calculateFreightContext = (CalculateFreightContext) context
				.getInnerContext(CalculateFreightContext.class);
		SaveWaybillReceiptContext saveWaybillReceiptContext = (SaveWaybillReceiptContext) context
				.getInnerContext(SaveWaybillReceiptContext.class);
		
		try {
			waybillIncomeFeeDao.insert(buildWaybillIncomeFee(saveWaybillReceiptContext, calculateFreightContext));
				SaveTackInfoContext saveTackInfoContext = (SaveTackInfoContext) context
						.getInnerContext(SaveTackInfoContext.class);
				BeanUtils.copyProperties(saveWaybillReceiptContext, saveTackInfoContext);
				Date date=new Date();
				saveTackInfoContext.setCompanyCode(calculateFreightContext.getCompanyCode());
				saveTackInfoContext.setContent(String.format("包裹已经完成收货，收货时间：%s，收货地点：%ss",DateUtils.getTimeStampStr(date) ,saveWaybillReceiptContext.getAddress()));
				saveTackInfoContext.setTrackTime(date);
			
			return true;
		} catch (Exception e) {
			throw TmsExceptionUtil.wrapException(e);
		}

	}
	
	private WaybillIncomeFeeDO buildWaybillIncomeFee(SaveWaybillReceiptContext saveWaybillReceiptContext,CalculateFreightContext calculateFreightContext){
		String recordSeq = TmsSerializeConstant.WAYBILL_INCOME_KEY+globalIdGeneratorUtil.getNextSeq(TmsSerializeConstant.WAYBILL_INCOME_SEQ_KEY, TmsSerializeConstant.SEQ_INCRBY);
		WaybillIncomeFeeDO waybillIncomeFeeDO = WaybillIncomeFeeDO.builder()
		.incomeCode(recordSeq)
		.waybillCode(saveWaybillReceiptContext.getWaybillCode())
		.companyCode(calculateFreightContext.getCompanyCode())
		.feeName("运费")
		.feeCode("FREIGHT")
		.predictAmount(saveWaybillReceiptContext.getFrozenAmount())
		.receivable(calculateFreightContext.getAmount())
		.receipts(calculateFreightContext.getAmount())
		.state(1)
		.build();
		waybillIncomeFeeDO.setIp(saveWaybillReceiptContext.getIp());
		waybillIncomeFeeDO.setGmtCreatePin(saveWaybillReceiptContext.getPin());
		waybillIncomeFeeDO.setGmtModifiedPin(saveWaybillReceiptContext.getPin());
		return waybillIncomeFeeDO;
	}

}
